package service

import (
	"github.com/gin-gonic/gin"
	"talentsystem/model"
	"github.com/astaxie/beego/orm"
)

func HaveResume (c* gin.Context){
	var param model.TypeId
	if err:=c.Bind(&param);err!=nil{
		c.JSON(400,gin.H{
			"status":400,
			"data":"参数格式不正确",
		})
		return
	}
	error,state:=PerfectResume(param.Id)
	if error!=nil{
		c.JSON(500, gin.H{
			"status": 500,
			"msg":    "系统错误,请刷新重试",
		})
		return
	}
	if state==2{
		c.JSON(200, gin.H{
			"status":200,
			"msg":    2,
		})
		return
	}
	c.JSON(200, gin.H{
		"status": 200,
		"msg":    1,
	})
	return
}
func DeliverResume (c*gin.Context){
	var param model.DeliverResume
	if err:=c.Bind(&param);err!=nil{
		c.JSON(400,gin.H{
			"status":400,
			"data":"参数格式不正确",
		})
		return
	}
	total,e:=orm.NewOrm().QueryTable("resume_log").Filter("account_id",param.Accountid).Filter("position_id",param.Positionid).Filter("date",param.Date).Count()
	if e!= nil{
		c.JSON(500, gin.H{
			"status": 500,
			"msg":    "系统错误,请刷新重试",
		})
		return
	}
	if total>0{
		c.JSON(403, gin.H{
			"status": 403,
			"msg":    "你今天已经投过该职位了",
		})
		return
	}

	o:=orm.NewOrm()
	resumelog:=new(model.ResumeLog)
	resumelog.Id=0
	resumelog.ResumeId=0
	resumelog.Status=0
	resumelog.AccountId=param.Accountid
	resumelog.PositionId=param.Positionid
	resumelog.Fphone=param.Fphone
	resumelog.Date=param.Date
	_,err:=o.Insert(resumelog)
	if err!= nil{
		c.JSON(500, gin.H{
			"status": 500,
			"msg":    "系统错误,请刷新重试",
		})
		return
	}
	c.JSON(200, gin.H{
		"status": 200,
		"msg":    "投递成功",
	})
	return
}
func PerfectResume (accountid int64) (err error,state int){
	num,err:=orm.NewOrm().QueryTable("resume").Filter("account_id",accountid).Count()
	if num ==0{
		return err,2
	}else{
		var result model.Resume
		err:=orm.NewOrm().QueryTable("resume").Filter("account_id",accountid).One(&result,"phone","user_name")
		if err!=nil{
			return err,0
		}
		if result.Phone==""||result.UserName==""{
			return err,2
		}
	}
	return err,1
}

func GetResumeLog(c*gin.Context){
	var param model.GetAccountApplyJob
	if err:=c.Bind(&param);err!=nil{
		c.JSON(400,gin.H{
			"status":400,
			"msg":"参数格式不正确",
		})
		return
	}
	offset:=(param.Page-1)*param.Pagesize
	var result []model.GetResumeLog
	_,err:=orm.NewOrm().Raw("SELECT resume_log.id,resume_log.account_id,resume_log.position_id,resume_log.fphone,resume_log.`status`,resume_log.date,resume_log.remark,account_apply_job.user_name,account_apply_job.phone,account_apply_job.sex,account_apply_job.age,position.company,position.position FROM position,account_apply_job,resume_log WHERE resume_log.account_id=account_apply_job.id AND resume_log.position_id=position.id ORDER BY resume_log.id DESC LIMIT ? OFFSET ?",param.Pagesize,offset).QueryRows(&result)
	if err!=nil{
		c.JSON(500,gin.H{
			"status":500,
			"msg":"系统错请刷新重试",
		})
		return
	}
	num,err:=orm.NewOrm().QueryTable("resume_log").Count()
	if err!=nil{
		c.JSON(500,gin.H{
			"status":500,
			"msg":"系统错请刷新重试",
		})
		return
	}
	c.JSON(200,gin.H{
		"status":200,
		"msg":result,
		"total":num,
	})
	return
}
func UpdateResumeLogStatus (c*gin.Context){
	var param model.TypeUpdateResumeLog
	if err:=c.Bind(&param);err!=nil{
		c.JSON(400,gin.H{
			"status":400,
			"msg":"参数格式不正确",
		})
		return
	}
	err,state:=UpdateResumeLogTools(param.Status,param.Id,param.Remark)
	if err!=nil {
		c.JSON(500,gin.H{
			"status":500,
			"msg":"系统错请刷新重试",
		})
		return
	}
	c.JSON(200,gin.H{
		"status":200,
		"msg":state,
	})
}
func GetAAJResumeLog(c*gin.Context){
	var param model.TypeId
	if err:=c.Bind(&param);err!=nil{
		c.JSON(400,gin.H{
			"status":400,
			"msg":"参数格式不正确",
		})
		return
	}
	var msg []model.GetAJJLogMsg
	num,err:=orm.NewOrm().Raw("SELECT position.position,position.company,position.area,position.wages,resume.user_name,position.work_time,position.edueational,resume_log.`status`,resume_log.date,resume_log.remark FROM position,resume,resume_log WHERE resume_log.account_id=resume.account_id AND resume_log.position_id=position.id AND resume_log.account_id=? ORDER BY resume_log.id DESC",param.Id).QueryRows(&msg)
	if err!=nil {
		c.JSON(500,gin.H{
			"status":500,
			"msg":"系统错请刷新重试",
		})
		return
	}
	var result []model.GetAJJLogMsg
	total,err:=orm.NewOrm().Raw("SELECT position.position,position.company,position.area,position.wages,resume.user_name,position.work_time,position.edueational,enrol_log.`status`,enrol_log.date,enrol_log.remark FROM position,resume,enrol_log WHERE enrol_log.account_id=resume.account_id AND enrol_log.position_id=position.id AND enrol_log.account_id=? ORDER BY enrol_log.id DESC",param.Id).QueryRows(&result)
	if err!=nil {
		c.JSON(500,gin.H{
			"status":500,
			"msg":"系统错请刷新重试",
		})
		return
	}
	var aajlog model.AAJLog
	for range msg{
		aajlog.ResumeLog=msg
	}
	for range result{
		aajlog.EnrloLog=result
	}

	c.JSON(200,gin.H{
		"status":200,
		"data":aajlog,
		"total":num+total,
	})
	return
}
func GetAAJLogMsg(id int64)(err error,msg model.ResumeLog){
	_,err=orm.NewOrm().QueryTable("resume_log").Filter("account_id").All(&msg)
	return err,msg
}
func UpdateResumeLogTools(state int64,id int64,remark string)(err error,status int64){
	o:=orm.NewOrm()
	_,err=o.QueryTable("resume_log").Filter("id",id).Update(orm.Params{
		"Status":state,
		"Remark":remark,
	})
	if err!= nil{
		return err,0
	}
	return nil,1
}
